winsafe\shell\com_interfaces/ishellitemarray.rs
1#![allow(non_camel_case_types, non_snake_case)]
2
3use crate::decl::*;
4use crate::ole::privs::*;
5use crate::prelude::*;
6use crate::shell::{iterators::*, vts::*};
7
8com_interface! { IShellItemArray: "b63ea76d-1f85-456f-a19c-48159efa858b";
9 /// [`IShellItemArray`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nn-shobjidl_core-ishellitemarray)
10 /// COM interface.
11 ///
12 /// Automatically calls
13 /// [`IUnknown::Release`](https://learn.microsoft.com/en-us/windows/win32/api/unknwn/nf-unknwn-iunknown-release)
14 /// when the object goes out of scope.
15}
16
17impl shell_IShellItemArray for IShellItemArray {}
18
19/// This trait is enabled with the `shell` feature, and provides methods for
20/// [`IShellItemArray`](crate::IShellItemArray).
21///
22/// Prefer importing this trait through the prelude:
23///
24/// ```no_run
25/// use winsafe::prelude::*;
26/// ```
27pub trait shell_IShellItemArray: ole_IUnknown {
28 /// Returns an iterator over the [`IShellItem`](crate::IShellItem) elements
29 /// by calling
30 /// [`IShellItemArray::GetCount`](crate::prelude::shell_IShellItemArray::GetCount)
31 /// and
32 /// [`IShellItemArray::GetItemAt`](crate::prelude::shell_IShellItemArray::GetItemAt)
33 /// consecutively.
34 ///
35 /// # Examples
36 ///
37 /// Iterating over the [`IShellItem`](crate::IShellItem) objects:
38 ///
39 /// ```no_run
40 /// use winsafe::{self as w, prelude::*, co};
41 ///
42 /// let ish_arr: w::IShellItemArray; // initialized somewhere
43 /// # let ish_arr = unsafe { w::IShellItemArray::null() };
44 ///
45 /// for ish_item in ish_arr.iter()? {
46 /// let ish_item = ish_item?;
47 /// println!("Path: {}",
48 /// ish_item.GetDisplayName(co::SIGDN::FILESYSPATH)?);
49 /// }
50 /// # w::HrResult::Ok(())
51 /// ```
52 ///
53 /// Collecting the file paths into a [`Vec`](std::vec::Vec):
54 ///
55 /// ```no_run
56 /// use winsafe::{self as w, prelude::*, co};
57 ///
58 /// let ish_arr: w::IShellItemArray; // initialized somewhere
59 /// # let ish_arr = unsafe { w::IShellItemArray::null() };
60 ///
61 /// let paths = ish_arr.iter()?
62 /// .map(|shi| {
63 /// let shi = shi?;
64 /// let name = shi.GetDisplayName(co::SIGDN::FILESYSPATH)?;
65 /// Ok(name)
66 /// })
67 /// .collect::<w::HrResult<Vec<_>>>()?;
68 /// # w::HrResult::Ok(())
69 /// ```
70 #[must_use]
71 fn iter(&self) -> HrResult<impl DoubleEndedIterator<Item = HrResult<IShellItem>> + '_> {
72 Ok(IshellitemarrayIter::new(self)?)
73 }
74
75 /// [`IShellItemArray::GetCount`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ishellitemarray-getcount)
76 /// method.
77 #[must_use]
78 fn GetCount(&self) -> HrResult<u32> {
79 let mut count = 0u32;
80 ok_to_hrresult(unsafe { (vt::<IShellItemArrayVT>(self).GetCount)(self.ptr(), &mut count) })
81 .map(|_| count)
82 }
83
84 /// [`IShellItemArray::GetItemAt`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ishellitemarray-getitemat)
85 /// method.
86 ///
87 /// Prefer using
88 /// [`IShellItemArrayT::iter`](crate::prelude::shell_IShellItemArray::iter).
89 #[must_use]
90 fn GetItemAt(&self, index: u32) -> HrResult<IShellItem> {
91 let mut queried = unsafe { IShellItem::null() };
92 ok_to_hrresult(unsafe {
93 (vt::<IShellItemArrayVT>(self).GetItemAt)(self.ptr(), index, queried.as_mut())
94 })
95 .map(|_| queried)
96 }
97}